﻿using System.Collections.Generic;
using Common;
using Microsoft.Practices.CompositeWeb;
using Microsoft.Practices.ObjectBuilder;

namespace CSC440TeamOne.MyHealth.Views
{
    public class HealthQuestionairePresenter : Presenter<IHealthQuestionaireView>
    {
        private List<Question> AllQuestions;

        // NOTE: Uncomment the following code if you want ObjectBuilder to inject the module controller
        //       The code will not work in the Shell module, as a module controller is not created by default
        //
        private IMyHealthController _controller;
        public HealthQuestionairePresenter([CreateNew] IMyHealthController controller)
        {
            _controller = controller;
        }

        public override void OnViewLoaded()
        {
            // TODO: Implement code that will be executed every time the view loads
            //if (AllQuestions != null)
            //{
            //    AllQuestions.Clear();
            //}
            AllQuestions = _controller.LoadAllQuestions();
        }

        public override void OnViewInitialized()
        {
            // TODO: Implement code that will be executed the first time the view loads
            
        }

        // TODO: Handle other view events and set state in the view
        public List<Question> getQuestions(int questionPage)
        {
            List<Question> questions = new List<Question>();

            foreach (Question q in AllQuestions)
            {
                if (q.PageID == questionPage)
                {
                    questions.Add(q);
                }
            }

            return questions;
        }

        public void saveAnswer(int questionID, string answer)
        {
            _controller.SaveQuestionaireAnswer(questionID, answer);
        }


        public int getMaxPages()
        {
            int maxpage = 0;

            foreach (Question q in AllQuestions)
            {
                if (q.PageID > maxpage)
                {
                    maxpage = q.PageID;
                }
            }

            return maxpage;
        }

        public User LoadCurrentUser()
        {
            return _controller.LoadCurrentUser();
        }
    }
}




